package com.heima.wemedia.listener;

import com.heima.model.wemedia.pojo.WmNews;
import com.heima.wemedia.config.RabbitMqConfig;
import com.heima.wemedia.service.WmNewSVerfiyService;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class PublishListener {

    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private WmNewSVerfiyService wmNewsVerfiyService;

    @RabbitListener(queues = RabbitMqConfig.DEAD_QUEUE_NAME)
    public void receiveMessage(String newsId){
        //1.根据自媒体文章id查询文章对象
        WmNews wmNews = wmNewsService.getById(newsId);
        //2.判断status是否为8
        if (wmNews != null) {
            if (wmNews.getStatus() == WmNews.Status.SUCCESS.getCode() && wmNews.getPublishTime().before(new Date())) {
                //发布文章
                wmNewsVerfiyService.publishArticle(wmNews);
            }
        }
    }
}
